/**
 * @Author lilinjie
 * @Description: 商品管理hook
 */
import { useCuModal } from '@/hooks/common/useCuModal'
import { addCategory } from '@/api/category'
import { insertProduct, updateProduct } from '@/api/product'
import { message } from 'ant-design-vue'

export const useProductSkuTable = ({ handleSearch }) => {
  // 弹窗部分
  const {
    cuFilterForm: skuFilterForm,
    modelData: skuModelData,
    isEdit: skuOnEdit,
    onClickClose: skuOnClickClose,
    onClickConfirm: skuOnClickConfirm,
    onClickCreate: skuOnClickCreate,
    onClickEdit: skuOnClickEdit,
    onClickView: skuOnClickView
  } = useCuModal({
    defaultCuFilterForm: {
      id: undefined,
      name: undefined,
      skuId: undefined,
      skuName: undefined,
      imgUrl: undefined,
      origin: undefined,
      stockNum: undefined,
      salePrice: undefined,
      saleNum: undefined,
      weight: undefined,
    },
    create({ cuFilterForm, onSuccess }) {
      console.log('create', cuFilterForm)
      insertProduct({ product: null, productSkuList: [{...cuFilterForm,
          saleNum: parseInt(cuFilterForm.saleNum),
          stockNum: parseInt(cuFilterForm.stockNum),
          weight: parseInt(cuFilterForm.weight),
          salePrice: parseInt(cuFilterForm.salePrice),
      }] }).then(res => {
        console.log(res)
        onSuccess()
        handleSearch()
      })
    },
    update({ cuFilterForm, currentId, onSuccess }) {
      updateProduct({ product: { id: cuFilterForm.productId }, productSkuList: [cuFilterForm] }).then(res => {
        console.log(res)
        onSuccess()
        handleSearch()
      }).catch(err => {
        console.log(err)
        message.error('修改失败')
      })
      console.log('update', cuFilterForm)

    }
  })

  return {
    skuFilterForm,
    skuModelData,
    skuOnEdit,
    skuOnClickClose,
    skuOnClickConfirm,
    skuOnClickCreate,
    skuOnClickEdit,
    skuOnClickView
  }
}
